package com.coding365.rpcprovider.zookeeper;

import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZookeeperUtil {
    @Value("${zookeeper.ip}")
    private String zIp;
    @Value("${zookeeper.port}")
    private Integer zPort;
    public void registryIpAndPort(String ip,Integer port){
        ZkClient zkClient = new ZkClient(zIp + ":" + zPort);
        Map ipAndPort = new HashMap();
        ipAndPort.put("ip",ip);
        ipAndPort.put("port",port);
        String parentPath = "/server";
        String path = "/server/" + ip + ":" + port;
        if (!zkClient.exists(parentPath)){
            zkClient.createPersistent(parentPath);
        }
        if (!zkClient.exists(path)){
            zkClient.createEphemeral(path);
        }
        zkClient.writeData(path,ipAndPort);
        Map data = zkClient.readData(path);
        System.out.println(data);
    }
}
